package com.byit.controller.quartz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.ArrayUtils;
import com.byit.constant.DefaultValue;
import com.byit.domain.quartz.Job;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.IJobService;
import com.byit.shrio.session.SessionHelper;
import com.byit.utils.ConvertUtil;
import com.byit.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 调度任务信息操作处理
 *
 * @author morej
 */
@RestController
@RequestMapping("/api/quartz/job")
public class JobController{
	@Autowired
	private IJobService jobService;

	@Log(title = "定时任务", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
	@GetMapping("/list")
	public Object list(Job job,
					   @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
					   @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
		Page<Job> pageInfo = new Page<>(page, limit);
		EntityWrapper<Job> wrapper = new EntityWrapper<>();
		wrapper.setSqlSelect("job_id as jobId,job_name as jobName,job_group as jobGroup,method_name as methodName,params,cron_expression as cronExpression,status");
		wrapper.eq("is_del",0);
		if (StringUtil.isNotNulls(job.getJobName())) {
			wrapper.eq("job_name",job.getJobName());
		}

		if (StringUtil.isNotNulls(job.getMethodName())) {
			wrapper.eq("method_name",job.getMethodName());
		}
		if (StringUtil.isNotNulls(job.getStatus())) {
			wrapper.eq("status",job.getStatus());
		}
		Page<Job> jobPage = jobService.selectPage(pageInfo, wrapper);
		return MessageResult.warpper(PromptInfo.SYS_SECCUSS, jobPage);
	}

	@Log(title = "定时任务", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
	@PostMapping("/remove")
	public Object remove(String ids) {
		try {
			if (StringUtil.isNotEmpty(ids)){
				jobService.deleteJobByIds(ids);
				return MessageResult.success();
			}else {
				return MessageResult.error("请传入需要删除的id");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return MessageResult.error();
		}
	}

	/**
	 * 任务调度状态修改
	 */
	@Log(title = "定时任务", operate = OperatorType.DATA_UPDATE, channel = ChannelType.WEB_INTER)
	@PostMapping("/changeStatus")
	public Object changeStatus(Job job,HttpSession session) {
		Long userId = SessionHelper.getUserId(session);
		job.setUpdateBy(userId.toString());
		job.setUpdateTime(new Date());
		if (jobService.changeStatus(job)>0) {
			return MessageResult.success();
		}
		return MessageResult.error();
	}

	/**
	 * 任务调度立即执行一次
	 */
	@Log(title = "定时任务", operate = OperatorType.DATA_UPDATE, channel = ChannelType.WEB_INTER)
	@PostMapping("/run")
	@ResponseBody
	public Object run(Job job,HttpSession session) {
		Long userId = SessionHelper.getUserId(session);
		job.setUpdateBy(userId.toString());
		job.setUpdateTime(new Date());
		if (jobService.run(job) > 0) {
			return MessageResult.success();
		}
		return MessageResult.error();
	}



	/**
	 * 保存调度
	 */
	@Log(title = "定时任务", operate = OperatorType.DATA_SAVEORUPDATE, channel = ChannelType.WEB_INTER)
	@PostMapping("/save")
	public Object save(Job job,HttpSession session) {
		Long userId = SessionHelper.getUserId(session);
		job.setUpdateBy(userId.toString());
		job.setUpdateTime(new Date());
		if (jobService.saveJobCron(job) > 0) {
			return MessageResult.success();
		}
		return MessageResult.error();
	}

	@Log(title = "定时任务", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
	@PostMapping("/details")
	public Object details(@RequestParam(name = "id") Integer id) {
        Job job = jobService.selectById(id);
        if (job.getIsDel() == 1) {
            return MessageResult.error("该数据已删除");
        }
        return MessageResult.success(job);
    }
}
